package web_programing.http;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author haomin
 * @date 2022/10/11 22:20
 **/
public class HttpServer {
    public HttpServer(){}

    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = new ServerSocket(80);

        while (true){
            Socket socket = serverSocket.accept();
            OutputStream os = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "utf-8"));

            String html = "<a href='https://www.baidu.com'>百度一下</a>";
            byte[] bytes = html.getBytes("utf-8");
            int contentLength = bytes.length;
            // Content-Type: application/octet-stream;
            String response = "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 49\r\n\r\n" + html;
            writer.println(response);
            writer.flush();
            socket.close();
        }
    }
}